package com.NyCloud.controller;


import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.NyCloud.entity.Company;
import com.NyCloud.entity.Demand;
import com.NyCloud.entity.Location;
import com.NyCloud.entity.vo.CompanyVo;
import com.NyCloud.service.ICompanyService;
import com.NyCloud.service.IDemandService;
import com.NyCloud.service.ILocationService;
import com.NyCloud.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者
 * @since 2024-07-26
 */
@RestController
@RequestMapping("/company")
@CrossOrigin
@Api(tags = "企业信息")
public class CompanyController {

    @Autowired
    private IUserService userService;

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private IDemandService demandService;

    @Autowired
    private ILocationService locationService;

    @GetMapping("/list")
    @ApiOperation("全部公司信息")
    public SaResult getCompanyList() {
        List<Company> companies = companyService.list();
        ArrayList<CompanyVo> companyVos = new ArrayList<>();
        for (Company company : companies) {
            CompanyVo companyVo = new CompanyVo();
            BeanUtils.copyProperties(company, companyVo);
            List<Demand> demandList = demandService.list(new QueryWrapper<Demand>()
                    .eq("company_id", company.getUserId()));
            StringBuilder demandInfo = new StringBuilder();
            for (Demand demand : demandList) {

                demandInfo.append(String.format("产品：%s,月需求 %.2f吨,年需求 %.2f吨;",
                        demand.getProductName(),
                        demand.getMonthlyDemand(),
                        demand.getYearlyDemand()));
            }
            Location location = locationService.getOne(new QueryWrapper<Location>()
                    .eq("user_id", company.getUserId()));
            companyVo.setLocation(location);
            companyVo.setDemandInfo(demandInfo.toString());
            companyVos.add(companyVo);
        }
        return SaResult.data(companyVos);
    }

    @GetMapping("/{userId}")
    @ApiOperation("获取公司信息")
    public SaResult getCompany(@PathVariable Integer userId) {
        Company company = companyService.getOne(new QueryWrapper<Company>()
                .eq("user_id", userId));
        return SaResult.data(company);
    }

    @PostMapping
    @ApiOperation("更新公司信息")
    public SaResult updateCompany(@RequestBody Company company) {
//        int id = StpUtil.getLoginIdAsInt();
//        company.setUserId(id);
        companyService.updateById(company);
        return SaResult.ok();
    }



}
